package com.gin.no_gay_alliance.itemdecoration;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * GridLayoutManager的分隔配置
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/3 16:15
 **/
public class GridItemDecoration extends RecyclerView.ItemDecoration {
    /**
     * 列数
     */
    private final int spanCount;

    public GridItemDecoration(int spanCount) {
        this.spanCount = spanCount;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        // 父元素宽度
        assert parent.getLayoutManager() != null;
        int parentWidth = parent.getLayoutManager().getWidth();
        int childWidth = view.getLayoutParams().width;
        // 间距宽度
        outRect.left = (parentWidth - childWidth * spanCount) / (spanCount + 1);
        outRect.top = 10;
    }
}
